package com.stupidzhe.vbreview.config;

import com.stupidzhe.vbreview.cache.StudentCache;
import com.stupidzhe.vbreview.cache.TeacherCache;
import com.stupidzhe.vbreview.interceptor.AdmStatusInterceptor;
import com.stupidzhe.vbreview.interceptor.StuStatusInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * Created by Mr.W on 2019/3/9.
 * 拦截器配置
 */
@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {

    private final TeacherCache teacherCache;
    private final StudentCache studentCache;

    @Autowired
    public InterceptorConfig(TeacherCache teacherCache, StudentCache studentCache) {
        this.teacherCache = teacherCache;
        this.studentCache = studentCache;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/templates/")
                .addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AdmStatusInterceptor(teacherCache)).
                addPathPatterns("/adm/**")
                .excludePathPatterns("/adm/project/file/**")
                .excludePathPatterns("/adm/status/**")
                .excludePathPatterns("/adm/login");
        registry.addInterceptor(new StuStatusInterceptor(studentCache)).addPathPatterns("/stu/**")
                .excludePathPatterns("/stu/login/**")
                .excludePathPatterns("/stu/register/**")
                .excludePathPatterns("/stu/status/check/**");
//                .excludePathPatterns("/usr/top/ali_pay.asyn.callback");
    }
}
